/*
Copyright (c) 2009, Luke Berndt
All rights reserved.
 
 * More Information: http://www.tagflo.com

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
  and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of RoboTastic Software nor the names of its contributors may be used to endorse or 
  promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Threading;
using System.IO;

namespace TagFlo
{
    public enum StatusUpdateType
    {
        rating,
        addTag,
        addFile,
        addDirectory,
        deleteFile,
        deleteDirectory,
        information,
        exportFile,
        exportError
    }

    class StatusItem
    {
        StatusUpdateType updateType;
        string text;
        //BitmapImage image;
        byte[] imageArray;

        public StatusItem(StatusUpdateType ut, string t)
        {
            updateType = ut;
            text = t;
            imageArray = null;
        }

      /*  public StatusItem(StatusUpdateType ut, string t, BitmapImage i)
        {
            updateType = ut;
            text = t;
            image = i;
        }*/

        public StatusItem(StatusUpdateType ut, string t, byte[] ba)
        {
            updateType = ut;
            text = t;
            imageArray = ba;

        }

        public string Text
        {
            get { return text; }

        }
        public byte[] ImageArray
        {
            get { return imageArray; }

        }
        public StatusUpdateType UpdateType
        {
            get { return updateType; }
        }
    }

    class StatusWorker
    {
        private static readonly object _lockStack = new object();

        private static AutoResetEvent _updateStatusEvent = new AutoResetEvent(false);

        private static readonly Stack<StatusItem> _statusStack = new Stack<StatusItem>();

        private delegate void RaiseStatusPopupCallback(string text, byte[] imageArray);
        private delegate void LowerStatusPopupCallback();
       
        private MainWindow parent;


        public void UpdateStatusThread()
        {
            StatusItem item=null;
            bool moreWork = false;
            do
            {
                _updateStatusEvent.WaitOne();
                do
                {
                    try
                    {

                        lock (_lockStack)
                        {
                            if (_statusStack.Count > 0)
                            {
                                item = _statusStack.Pop();
                            }
                        }

                        if (item != null)
                        {
                            RaiseStatusPopupCallback raisePopup = new RaiseStatusPopupCallback(parent.RaiseStatusPopup);

                            parent.Dispatcher.BeginInvoke(DispatcherPriority.Send, raisePopup, item.Text, item.ImageArray);

                        }

                        Thread.Sleep(10);
                        lock (_lockStack)
                        {
                            if (_statusStack.Count == 0)
                            {
                                moreWork = false;
                            }
                            else
                            {
                                moreWork = true;
                            }
                        }


                    }
                    catch
                    {
                    }
                } while (moreWork);
                Thread.Sleep(1000);
                LowerStatusPopupCallback lowerPopup = new LowerStatusPopupCallback(parent.LowerStatusPopup);
                parent.Dispatcher.BeginInvoke(DispatcherPriority.Send, lowerPopup);
                            
            } while (true);
        }

        public void AddFileUpdate(string text, byte[] ba )
        {
           
          /*  MemoryStream stream = new MemoryStream(ba);

            
             BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.CacheOption = BitmapCacheOption.OnLoad;
            bi.DecodePixelHeight = 125;
            bi.DecodePixelWidth = 125;
            bi.StreamSource = stream;
            bi.EndInit();*/
            
            StatusItem item = new StatusItem(StatusUpdateType.addFile, text, ba);
            AddStatusUpdate(item);
        }

        public void AddInformationUpdate(string text)
        {
            StatusItem item = new StatusItem(StatusUpdateType.information, text);
            AddStatusUpdate(item);
        }

        public void AddRatingUpdate(string text)
        {
            StatusItem item = new StatusItem(StatusUpdateType.rating, text);
            AddStatusUpdate(item);
        }

        public void AddStatusUpdate(StatusItem item)
        {
            lock (_lockStack)
            {
                _statusStack.Push(item);
            }
            _updateStatusEvent.Set();
        }

        public StatusWorker(MainWindow mWindow)
        {
            parent = mWindow;

        }
    }
}
